<template>
  <div class="task-home">
    <!-- 欢迎横幅 -->
    <div class="welcome-banner">
      <div class="banner-content">
        <div class="welcome-text">
          <h1>学习任务中心</h1>
          <p>制定学习计划，完成学习目标，提升技能水平</p>
        </div>
        <div class="banner-stats">
          <div class="stat-item">
            <div class="stat-number">{{ stats.completedTasks }}</div>
            <div class="stat-label">已完成任务</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalTasks }}</div>
            <div class="stat-label">总任务数</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ stats.completionRate }}%</div>
            <div class="stat-label">完成率</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 今日任务 -->
    <div class="daily-tasks">
      <div class="section-header">
        <h2>今日任务</h2>
        <span class="task-count">{{ todayTasks.length }}个任务</span>
      </div>
      
      <div class="tasks-grid">
        <div 
          v-for="task in todayTasks" 
          :key="task.id"
          class="task-card"
          :class="{ completed: task.completed }"
          @click="viewTask(task)"
        >
          <div class="task-header">
            <div class="task-type">
              <el-tag :type="getTaskTypeColor(task.type)" size="small">
                {{ getTaskTypeName(task.type) }}
              </el-tag>
            </div>
            <div class="task-status">
              <el-icon v-if="task.completed" class="completed-icon">
                <CircleCheck />
              </el-icon>
              <el-icon v-else class="pending-icon">
                <Clock />
              </el-icon>
            </div>
          </div>
          
          <div class="task-content">
            <h3 class="task-title">{{ task.title }}</h3>
            <p class="task-description">{{ task.description }}</p>
            
            <div class="task-meta">
              <div class="meta-item">
                <el-icon><Clock /></el-icon>
                <span>{{ task.estimatedTime }}分钟</span>
              </div>
              <div class="meta-item">
                <el-icon><Star /></el-icon>
                <span>{{ task.difficulty }}</span>
              </div>
            </div>
            
            <div class="task-progress" v-if="task.progress !== undefined">
              <div class="progress-info">
                <span>进度：{{ task.progress }}%</span>
              </div>
              <el-progress 
                :percentage="task.progress" 
                :show-text="false"
                :stroke-width="6"
              />
            </div>
          </div>
          
          <div class="task-footer">
            <el-button 
              :type="task.completed ? 'success' : 'primary'" 
              size="small"
              @click.stop="handleTaskAction(task)"
            >
              {{ task.completed ? '已完成' : '开始学习' }}
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 学习路径 -->
    <div class="learning-paths">
      <div class="section-header">
        <h2>学习路径</h2>
        <el-button text @click="viewAllPaths">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="paths-grid">
        <div 
          v-for="path in learningPaths" 
          :key="path.id"
          class="path-card"
          @click="viewPath(path)"
        >
          <div class="path-icon">
            <el-icon><Folder /></el-icon>
          </div>
          <div class="path-content">
            <h3 class="path-title">{{ path.title }}</h3>
            <p class="path-description">{{ path.description }}</p>
            <div class="path-progress">
              <div class="progress-info">
                <span>{{ path.completedTasks }}/{{ path.totalTasks }}个任务</span>
                <span>{{ Math.round(path.completedTasks / path.totalTasks * 100) }}%</span>
              </div>
              <el-progress 
                :percentage="Math.round(path.completedTasks / path.totalTasks * 100)" 
                :show-text="false"
                :stroke-width="6"
              />
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 推荐任务 -->
    <div class="recommended-tasks">
      <div class="section-header">
        <h2>推荐任务</h2>
        <el-button text @click="viewAllTasks">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="tasks-list">
        <div 
          v-for="task in recommendedTasks" 
          :key="task.id"
          class="task-item"
          @click="viewTask(task)"
        >
          <div class="task-info">
            <div class="task-title">{{ task.title }}</div>
            <div class="task-meta">
              <el-tag :type="getTaskTypeColor(task.type)" size="small">
                {{ getTaskTypeName(task.type) }}
              </el-tag>
              <span class="task-difficulty">{{ task.difficulty }}</span>
              <span class="task-time">{{ task.estimatedTime }}分钟</span>
            </div>
          </div>
          
          <div class="task-actions">
            <el-button size="small" @click.stop="viewTask(task)">
              查看详情
            </el-button>
            <el-button size="small" type="primary" @click.stop="startTask(task)">
              开始学习
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 学习统计 -->
    <div class="learning-stats">
      <div class="section-header">
        <h2>学习统计</h2>
      </div>
      
      <div class="stats-container">
        <div class="stats-chart">
          <div class="chart-title">本周学习进度</div>
          <div class="chart-placeholder">
            <el-icon><TrendCharts /></el-icon>
            <span>学习进度图表</span>
          </div>
        </div>
        
        <div class="stats-details">
          <div class="detail-item">
            <div class="detail-label">本周完成任务</div>
            <div class="detail-value">{{ weeklyStats.completedTasks }}个</div>
          </div>
          <div class="detail-item">
            <div class="detail-label">累计学习时间</div>
            <div class="detail-value">{{ weeklyStats.totalTime }}小时</div>
          </div>
          <div class="detail-item">
            <div class="detail-label">平均每日学习</div>
            <div class="detail-value">{{ weeklyStats.averageDaily }}分钟</div>
          </div>
          <div class="detail-item">
            <div class="detail-label">连续学习天数</div>
            <div class="detail-value">{{ weeklyStats.streakDays }}天</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  CircleCheck, Clock, Star, ArrowRight, Folder, TrendCharts
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 响应式数据
const stats = ref({
  completedTasks: 28,
  totalTasks: 45,
  completionRate: 62
})

const todayTasks = ref([
  {
    id: 1,
    title: 'JavaScript基础语法',
    description: '学习JavaScript的基本语法和数据类型',
    type: 'video',
    difficulty: '简单',
    estimatedTime: 30,
    progress: 75,
    completed: false
  },
  {
    id: 2,
    title: 'HTML结构设计',
    description: '掌握HTML标签的使用和页面结构设计',
    type: 'practice',
    difficulty: '简单',
    estimatedTime: 45,
    completed: true
  },
  {
    id: 3,
    title: 'CSS样式布局',
    description: '学习CSS布局技巧和响应式设计',
    type: 'project',
    difficulty: '中等',
    estimatedTime: 60,
    progress: 30,
    completed: false
  }
])

const learningPaths = ref([
  {
    id: 1,
    title: '前端开发基础',
    description: '从零开始学习前端开发技术',
    completedTasks: 8,
    totalTasks: 15
  },
  {
    id: 2,
    title: 'JavaScript进阶',
    description: '深入学习JavaScript高级特性',
    completedTasks: 5,
    totalTasks: 12
  },
  {
    id: 3,
    title: 'Vue.js框架',
    description: '掌握Vue.js框架开发技能',
    completedTasks: 3,
    totalTasks: 10
  }
])

const recommendedTasks = ref([
  {
    id: 4,
    title: 'React组件开发',
    type: 'video',
    difficulty: '中等',
    estimatedTime: 90
  },
  {
    id: 5,
    title: 'Node.js后端开发',
    type: 'practice',
    difficulty: '中等',
    estimatedTime: 120
  },
  {
    id: 6,
    title: '数据库设计',
    type: 'theory',
    difficulty: '困难',
    estimatedTime: 75
  }
])

const weeklyStats = ref({
  completedTasks: 12,
  totalTime: 8.5,
  averageDaily: 73,
  streakDays: 5
})

// 方法
const getTaskTypeColor = (type) => {
  const colors = {
    'video': 'primary',
    'practice': 'success',
    'project': 'warning',
    'theory': 'info'
  }
  return colors[type] || 'default'
}

const getTaskTypeName = (type) => {
  const names = {
    'video': '视频学习',
    'practice': '实践练习',
    'project': '项目实战',
    'theory': '理论学习'
  }
  return names[type] || '未知'
}

const viewTask = (task) => {
  router.push(`/task/detail/${task.id}`)
}

const handleTaskAction = (task) => {
  if (task.completed) {
    ElMessage.info('任务已完成')
  } else {
    startTask(task)
  }
}

const startTask = (task) => {
  router.push(`/task/learn/${task.id}`)
}

const viewPath = (path) => {
  router.push(`/task/path/${path.id}`)
}

const viewAllPaths = () => {
  router.push('/task/paths')
}

const viewAllTasks = () => {
  router.push('/task/list')
}

onMounted(() => {
  // 初始化数据
})
</script>

<style lang="scss" scoped>
.task-home {
  min-height: 100vh;
  background: var(--el-bg-color-page);
}

.welcome-banner {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 60px 0;
}

.banner-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  @media (max-width: 768px) {
    flex-direction: column;
    gap: 30px;
    text-align: center;
  }
}

.welcome-text {
  h1 {
    font-size: 3rem;
    font-weight: 700;
    margin-bottom: 16px;
  }
  
  p {
    font-size: 1.25rem;
    opacity: 0.9;
  }
}

.banner-stats {
  display: flex;
  gap: 40px;
  
  @media (max-width: 768px) {
    gap: 20px;
  }
}

.stat-item {
  text-align: center;
  
  .stat-number {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 4px;
  }
  
  .stat-label {
    font-size: 0.9rem;
    opacity: 0.8;
  }
}

.daily-tasks,
.learning-paths,
.recommended-tasks,
.learning-stats {
  max-width: 1200px;
  margin: 0 auto 60px;
  padding: 0 20px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  
  h2 {
    font-size: 24px;
    font-weight: 600;
    color: var(--el-text-color-primary);
  }
  
  .task-count {
    font-size: 14px;
    color: var(--el-text-color-secondary);
  }
}

.tasks-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
}

.task-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  }
  
  &.completed {
    background: var(--el-color-success-light-9);
    border: 1px solid var(--el-color-success-light-5);
  }
}

.task-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.task-status {
  .completed-icon {
    color: var(--el-color-success);
    font-size: 20px;
  }
  
  .pending-icon {
    color: var(--el-color-warning);
    font-size: 20px;
  }
}

.task-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--el-text-color-primary);
}

.task-description {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-bottom: 16px;
  line-height: 1.5;
}

.task-meta {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
  
  .meta-item {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    color: var(--el-text-color-placeholder);
    
    .el-icon {
      color: var(--el-color-primary);
    }
  }
}

.task-progress {
  margin-bottom: 16px;
  
  .progress-info {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 12px;
    color: var(--el-text-color-secondary);
  }
}

.task-footer {
  text-align: right;
}

.paths-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
}

.path-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  }
}

.path-icon {
  width: 48px;
  height: 48px;
  background: var(--el-color-primary-light-9);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
  
  .el-icon {
    font-size: 24px;
    color: var(--el-color-primary);
  }
}

.path-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--el-text-color-primary);
}

.path-description {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-bottom: 16px;
  line-height: 1.5;
}

.path-progress {
  .progress-info {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 12px;
    color: var(--el-text-color-secondary);
  }
}

.tasks-list {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.task-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid var(--el-border-color-extra-light);
  cursor: pointer;
  transition: background-color 0.3s ease;
  
  &:hover {
    background: var(--el-fill-color-extra-light);
  }
  
  &:last-child {
    border-bottom: none;
  }
}

.task-info {
  flex: 1;
  
  .task-title {
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 8px;
    color: var(--el-text-color-primary);
  }
  
  .task-meta {
    display: flex;
    gap: 12px;
    align-items: center;
    
    .task-difficulty,
    .task-time {
      font-size: 12px;
      color: var(--el-text-color-placeholder);
    }
  }
}

.task-actions {
  display: flex;
  gap: 8px;
}

.stats-container {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 40px;
  
  @media (max-width: 1024px) {
    grid-template-columns: 1fr;
  }
}

.stats-chart {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  
  .chart-title {
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 16px;
    color: var(--el-text-color-primary);
  }
  
  .chart-placeholder {
    height: 200px;
    background: var(--el-fill-color-extra-light);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: var(--el-text-color-placeholder);
    
    .el-icon {
      font-size: 32px;
    }
  }
}

.stats-details {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--el-border-color-extra-light);
  
  &:last-child {
    border-bottom: none;
  }
  
  .detail-label {
    font-size: 14px;
    color: var(--el-text-color-secondary);
  }
  
  .detail-value {
    font-size: 16px;
    font-weight: 600;
    color: var(--el-color-primary);
  }
}

@media (max-width: 768px) {
  .tasks-grid,
  .paths-grid {
    grid-template-columns: 1fr;
  }
  
  .task-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
    
    .task-actions {
      width: 100%;
      justify-content: flex-end;
    }
  }
}
</style>
